package com.cs474.cupcakes7;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddRecipe extends Activity {
	//Adapters for the spinners
	private ArrayAdapter<CharSequence> adapterForSpinner1;
	private ArrayAdapter<CharSequence> adapterForSpinner2;
	private ArrayAdapter<CharSequence> adapterForSpinner3;
	//Ingredient spinners
	private Spinner ingr1Spinner;
	private Spinner ingr2Spinner;
	private Spinner ingr3Spinner;
	//Button to add
	private Button addRecipe;
	//Edit text fields
	private EditText recipeName;
	private EditText recipeInstructions;
	//Error and success messages
	private Toast errorMessage;
	private Toast successMessage;
	
	
	public void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        
	        //Set the layout for the ingredient
	        setContentView(R.layout.add_recipe);
	        //Initialize the messages
	        errorMessage = Toast.makeText(this, "One of the fields is empty! Try again!", 
    				Toast.LENGTH_LONG);
	        
	        //Find the text fields...
	        recipeName = 			(EditText) findViewById(R.id.recipeName);
	        recipeInstructions = 	(EditText) findViewById(R.id.recipeInstructions);

	        
	        //Find the ingredient spinners
	        ingr1Spinner = (Spinner) findViewById(R.id.spinnerIngredient1);
	        ingr2Spinner = (Spinner) findViewById(R.id.spinnerIngredient2);
	        ingr3Spinner = (Spinner) findViewById(R.id.spinnerIngredient3);
	        //Create an adapter for the spinner
	        adapterForSpinner1 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	        adapterForSpinner2 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	        adapterForSpinner3 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	        //set the adapter for for the spinners
	        ingr1Spinner.setAdapter(adapterForSpinner1);
	        ingr2Spinner.setAdapter(adapterForSpinner2);
	        ingr3Spinner.setAdapter(adapterForSpinner3);
	        
	        addRecipe = (Button) findViewById(R.id.addRecipeButton);
	        
	        populateIngredientSpinners(adapterForSpinner1, adapterForSpinner2, adapterForSpinner3 );

	        
	        //Add a listener for the button...     
	    	addRecipe.setOnClickListener(new OnClickListener() {
	        	@Override
	        	public void onClick(View v) {
	        		String strRecipeName = recipeName.getText().toString();
	        		String strRecipeInstructions = recipeInstructions.getText().toString();
	        		
	        		
	        		//Check for array index out of bounds over here...
	        		
	        		String ingr1 = adapterForSpinner1.getItem(ingr1Spinner.getSelectedItemPosition()).toString();
	        		String ingr2 = adapterForSpinner2.getItem(ingr2Spinner.getSelectedItemPosition()).toString();
	        		String ingr3 = adapterForSpinner3.getItem(ingr3Spinner.getSelectedItemPosition()).toString();
	        		
	        		if (strRecipeName!=null) {
	        			
	        			
		        		successMessage = Toast.makeText(getApplicationContext(), 
		    	        		"Added Recipe:\nName: " + strRecipeName, Toast.LENGTH_LONG);
		        		
		        		successMessage.show();
		        		
	    	        }
	    	        else {
	    	        	errorMessage.show();
	    	        }	
	        	}
	        });      
	 }
	
	
	
	/*
	 * We need to populate this based on ingredients that we have...
	 */
	private void populateIngredientSpinners(ArrayAdapter<CharSequence> a1, ArrayAdapter<CharSequence> a2,
											ArrayAdapter<CharSequence> a3) 
	{
		a1.add("");
		a2.add("");
		a3.add("");

	}

	
	/*
	 *  Here is the function that interacts with the underlying
	 *  C++ code to add an ingredient to the list of ingredients.
	 */
	private void addRecipe(String name, int i) {
		
		//Go back 
		startActivity(new Intent(this, IngredientsList.class));
    }
	
	
	
}
